/**
 * 建议尽量全字段或者区域多字段进行匹配, 避免误触
 */

// require("ocr.js")
// require("utils.js")
// require("ggUtils.js")

var KS_CURRENT_FLAG = '1'
var PACK_NAME_KS = 'com.smile.gifmaker' //'com.kuaishou.kgx.novel'

function skip_主界面的弹窗_KS() {
    printinfoOld('skip_主界面的弹窗_KS')
    var detects = opencv.findImagesEx('图色967291.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    agentOrHidClick(ocrTextMoreComplete('我知道了', '取消', '忽略'), 1, () => {
        agentOrHidback()
        agentOrHidClick(ocrText('不满意'))
        pressDect(ocrTextMoreComplete('我知道了', '取消', '忽略'))
        agentOrHidClick(ocrText('不喜欢该作者'))
    })
}

function skip_任务中心的弹窗_KS() {
    printinfoOld('skip_任务中心的弹窗_KS')
    //找图返回目标数组
    var detects = opencv.findImagesEx('图色131331.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    //找图返回目标数组
    var detects继续 = opencv.findImagesEx('图色224353.cv');
    if (detects继续 != null) {
        agentOrHidClick(detects继续[0])
    }


    var ocr1close = opencv.OCREx('图色514102.cv');
    if (ocr1close != null && ocr1close.getAllString() == 'x') {
        agentOrHidClick(ocr1close.getAllDetect()[0])
    }

    agentOrHidClick(ocrText('我知道了', '取消', '跳过'))
}

// 启动任务
function start_app_KS() {
    // app.closeApp('com.smile.gifmaker')
    sleep.millisecond(200)
    startApp('com.smile.gifmaker')

    // 跳过首页广告
    expectView(() => {
        return ocrText('首页')
    }, (times) => {
        skip_主界面的弹窗_KS()

        if (times > 6) {
            agentOrHidback()
            sleep.millisecond(500)
            // todo
        }
        sleep.millisecond(500)
    }, (findView) => {
        // agentOrHidClick(findView)
    }, null, 8)
}

function isIncenterPage_KS() {
    if (ocrText('任务中心', '点可领', '开宝箱', '我的现金', '日常任务', '去邀请', '现金')) {
        return true
    }

    return false
}

// 进入任务中心
function enter_任务中心_KS(skipFlag) {
    //宝箱
    var detects = opencv.findImagesEx('图色452471.cv');
    if (detects != null) {
        return
    }

    if (skipFlag == undefined) skipFlag = false
    // 进入任务中心
    expectView(() => {
        var detects红包 = opencv.findMultiColorEx('图色454291.cv');
        if (detects红包 != null) {
            return detects红包[0]
        }

        var detects = opencv.findImagesEx('图色362056.cv');
        if (detects != null) {
            return detects[0]
        }
        return null
    }, (times) => {
        skip_主界面的弹窗_KS()

        sleep.millisecond(500)
    }, (findView, stepTimes) => {
        if (stepTimes == 1) {
            skip_主界面的弹窗_KS()
        }
        agentOrHidClick(findView)
    }, () => {
        // 失败后的方案 或者其它方案进入
        // clickOffectY(3 / 8, 1, 60)
        //找图返回目标数组
        var detects = opencv.findImagesEx('图色533239.cv');
        if (detects == null) {
            detects = backUntilFindView(() => {
                return opencv.findImagesEx('图色533239.cv');
            }, 3)
        }

        if (detects != null) {
            agentOrHidClick(detects[0])

            //找图返回目标数组
            var flag = null
            var detects1 = opencv.findImagesEx('图色938707.cv');
            if (detects1 == null) {
                flag = ocrWaitForText('任务中心', 6)
            } else {
                flag = detects1[0]
            }
            agentOrHidClick(flag)
        }
    }, 5)

    if (!skipFlag) {
        // 期望标志位
        expectView(() => {
            return ocrText('我的金币', '日常任务', '领现金')
        }, (times) => {
            skip_任务中心的弹窗_KS()

            if (times > 6) {
                agentOrHidback()
                sleep.millisecond(500)
                // todo 未找到后 返回怎么做
            }
            sleep.millisecond(500)
        }, (findView) => {
            // agentClickRight(findView)
        }, null, 8)
    }

}

function open_签到_KS() {
    printinfoOld('open_签到_KS-------------------------------')

    if (hasExcuteFunc('open_签到_KS' + KS_CURRENT_FLAG)) {
        addExcuteFuncTimes('open_签到_KS' + KS_CURRENT_FLAG)
        if (getExcuteFuncTimes('open_签到_KS' + KS_CURRENT_FLAG) < 3) {
            watchKsGG_多次看广告(ocrTextMoreComplete('赚更多', '去赚更多'))
            addExcuteFuncTimes('open_签到_KS' + KS_CURRENT_FLAG)
        }
        return
    }

    if (getExcuteFuncTimes('open_签到_KS' + KS_CURRENT_FLAG) > 1) {
        sleep.second(2)
        skip_任务中心的弹窗_KS()
    }


    var qdFlag = ocrWaitForMoreTextMillisecond('立即签到', '立即领取', 8)
    if (qdFlag) {
        agentOrHidClick(qdFlag)
        setExcuteFunc('open_签到_KS' + KS_CURRENT_FLAG)
    } else {
        //立即领取
        var detects = opencv.findImagesEx('图色410891.cv');
        if (detects != null) {
            agentOrHidClick(detects[0])
            setExcuteFunc('open_签到_KS' + KS_CURRENT_FLAG)
            watchKsGG_多次看广告(ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 0.8, 4, '内容最高可', '看內容最高可得', '看广告'))
        }
    }

    agentOrHidClick(ocrText('开启提醒', '提醒我'), 2, () => {
        agentOrHidClick(ocrTextMoreComplete('始终允许', '允许'))
    })

    var qd_完成Flag = ocrTextMoreComplete('赚更多', '去赚更多')
    if (qd_完成Flag) {
        setExcuteFunc('open_签到_KS' + KS_CURRENT_FLAG)
        watchKsGG_多次看广告(qd_完成Flag)
    }

    watchKsGG_多次看广告(ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 0.8, 4, '内容最高可', '看內容最高可得', '看广告'))
    skip_任务中心的弹窗_KS()
}

function open_开宝箱_KS() {
    printinfoOld('open_开宝箱_KS-------------------------------')
    skip_任务中心的弹窗_KS()

    agentOrHidClick(ocrWaitForMoreTextMillisecond('点就领', '点可领', '立刻领', '开宝箱得金币', 6), 1, () => {
        watchKsGG_多次看广告(ocrWaitForMoreTextMillisecondInRegion(0, 0.5, 1, 0.8, 4, '内容最高可', '看內容最高可得', '看视频', '看广告'))
    })

    skip_任务中心的弹窗_KS()
}

function watch_看广告_KS() {
    printinfoOld('watch_看广告_KS-------------------------------')
    skip_任务中心的弹窗_KS()

    if (hasExcuteFunc('watch_看广告_KS' + KS_CURRENT_FLAG)) {
        return
    }

    move2Top(7, 300, '我的现金', '每天随时兑换')
    moveUp2FindWidTxtMore((findView) => {
        if (!hasText(findView, '金币购划算')) {
            watchKsGG_多次看广告(findView)
            addExcuteFuncTimes('watch_看广告_HG' + KS_CURRENT_FLAG)
        }
    }, '单日最高赚', '看广告得', '看视频得', '看广告', '金币购划算')

    // backUntilFindView(() => ocrText('日常任务', '详情▼', '现金:', '点就领', '立刻领'))
}

function go_吃饭_KS() {
    printinfoOld('go_吃饭_KS-------------------------------')
    skip_任务中心的弹窗_KS()

    if (hasExcuteFunc('go_吃饭_KS' + KS_CURRENT_FLAG)) {
        return
    }

    move2Top(7, 300, '我的现金', '每天随时兑换')
    moveUp2FindWidTxtMore((findView) => {
        agentOrHidClick(findView)
        agentOrHidClick(ocrWaitForMoreTextMillisecond('领取饭补'))
        watchKsGG_多次看广告(ocrWaitForMoreTextMillisecondInRegion(0.1, 0.4, 0.9, 0.96, 6, '看视频', '看广告'))
        if (ocrText('已完成')) {
            setExcuteFunc('go_吃饭_KS' + KS_CURRENT_FLAG)
        }
    }, '到饭点领')


    if (!backUntilFindView(() => ocrText('日常任务', '详情▼', '现金:', '点就领', '开宝箱'))) {
        enter_任务中心_KS(true)
    }

}


function go_点赞_KS() {
    printinfoOld('go_点赞_KS-------------------------------')

    if (hasExcuteFunc('go_点赞_KS' + KS_CURRENT_FLAG)) {
        return
    }

    skip_任务中心的弹窗_KS()

    move2Top(7, 300, '我的现金', '每天随时兑换')
    moveUp2FindWidTxtMore((findView) => {
        if (getExcuteFuncTimes('go_点赞_KS' + KS_CURRENT_FLAG) > 1) {
            if (isInNearBy(findView, '已完成', 30)) {
                setExcuteFunc('go_点赞_KS' + KS_CURRENT_FLAG)
                return
            }
        }


        agentOrHidClick(findView)
        if (ocrText('点赞1个作品', '去点赞')) {
            if (getExcuteFuncTimes('go_点赞_KS' + KS_CURRENT_FLAG) > 1) {
                setExcuteFunc('go_点赞_KS' + KS_CURRENT_FLAG)
            }
            return
        }

        ocrWaitForMoreTextMillisecond('分享', '精选', '首页', 3)

        var detectssp = opencv.findImagesEx('图色208716.cv');
        if (detectssp != null) {
            agentOrHidClick(detectssp[0])
        }


        //头像关注
        var detects1 = opencv.findImagesEx('图色971253.cv');
        if (detects1 != null) {
            agentOrHidClickOffectY(detects1[0], -140)
        }

        agentSwipBig()
        sleep.second(2)

        // 判断是否为红包界面
        var detectsflag = opencv.findImagesEx('图色362056.cv');
        if (detectsflag != null) {
            doubleClick(0.5, 0.5)
        } else {
            agentSwipBig()
        }


        enter_任务中心_KS(true)
        agentOrHidClick(ocrWaitForMoreTextMillisecond('知道了', '我知道', 6), 1, () => {
            setExcuteFunc('go_点赞_KS' + KS_CURRENT_FLAG)
        })


    }, '点赞1个作品', '去点赞')

    addExcuteFuncTimes('go_点赞_KS' + KS_CURRENT_FLAG)
    if (getExcuteFuncTimes('go_点赞_KS' + KS_CURRENT_FLAG) >= 3) {
        setExcuteFunc('go_点赞_KS' + KS_CURRENT_FLAG)
    }

    // if (!backUntilFindView(() => ocrText('日常任务', '详情▼', '现金:', '点就领', '开宝箱'))) {
    //     enter_任务中心_KS(true)
    // }
    // backUntilFindView(() => ocrText('去赚钱', '抵用金', '日常任务'))
}


function go_评论_KS() {
    printinfoOld('go_评论_KS-------------------------------')


    if (hasExcuteFunc('go_评论_KS' + KS_CURRENT_FLAG)) {
        return
    }
    skip_任务中心的弹窗_KS()
    move2Top(7, 300, '我的现金', '每天随时兑换')
    moveUp2FindWidTxtMore((findView) => {
        if (getExcuteFuncTimes('go_评论_KS' + KS_CURRENT_FLAG) > 1) {
            if (isInNearBy(findView, '已完成', 20)) {
                setExcuteFunc('go_评论_KS' + KS_CURRENT_FLAG)
                return
            }
        }
        addExcuteFuncTimes('go_评论_KS' + KS_CURRENT_FLAG)

        agentOrHidClick(findView)

        if (ocrText('评论1个作品', '去评论')) {
            if (getExcuteFuncTimes('go_评论_KS' + KS_CURRENT_FLAG) > 1) {
                setExcuteFunc('go_评论_KS' + KS_CURRENT_FLAG)
            }
            return
        }

        ocrWaitForMoreTextMillisecond('分享', '精选', '首页', 3)

        // agentOrHidClickOffectY(ocrText('万'), 100)
        // 进入视频
        var detectssp = opencv.findImagesEx('图色208716.cv');
        if (detectssp != null) {
            agentOrHidClick(detectssp[0])
        }

        //头像关注
        var detects1 = opencv.findImagesEx('图色971253.cv');
        if (detects1 != null) {
            agentOrHidClickOffectY(detects1[0], -300)
        }



        //找图返回目标数组
        var detectsbq = opencv.findImagesEx('图色146277.cv');
        if (detectsbq != null) {
            agentOrHidClick(detectsbq[0])
        }

        if (ocrText('仅作者的好友')) {
            enter_任务中心_KS(true)
            return
        }


        //发表情
        var detectsfbq = opencv.findImagesEx('图色390583.cv');
        if (detectsfbq != null) {
            agentOrHidClick(detectsfbq[0])
        }

        //找图返回目标数组
        var detectsfs = opencv.findImagesEx('图色980421.cv');
        if (detectsfs != null) {
            agentOrHidClick(detectsfs[0])
        }

        enter_任务中心_KS(true)

        agentOrHidClick(ocrWaitForMoreTextMillisecond('知道了', '我知道', 6), 1, () => {
            setExcuteFunc('go_评论_KS' + KS_CURRENT_FLAG)
        })


    }, '评论1个作品', '去评论')

    addExcuteFuncTimes('go_评论_KS' + KS_CURRENT_FLAG)
    if (getExcuteFuncTimes('go_评论_KS' + KS_CURRENT_FLAG) >= 3) {
        setExcuteFunc('go_评论_KS' + KS_CURRENT_FLAG)
    }
    // backUntilFindView(() => ocrText('日常任务', '详情▼', '现金:', '点就领', '立刻领'))
}


function go_搜索_KS() {
    printinfoOld('go_搜索_KS-------------------------------')
    skip_任务中心的弹窗_KS()

    if (hasExcuteFunc('go_搜索_KS' + KS_CURRENT_FLAG)) {
        return
    }

    move2Top(7, 300, '我的现金', '每天随时兑换')
    moveUp2FindWidTxtMore((findView) => {
        if (!hasText(findView, '金币购划算')) {
            agentOrHidClick(findView)

            //搜索
            var detectsss = opencv.findImagesEx('图色638296.cv');
            if (detectsss != null) {
                agentOrHidClick(detectsss[0])

                sleep.second(3)
                //找图返回目标数组
                var detectsgg = opencv.findImagesEx('图色961209.cv');
                if (detectsgg != null) {
                    agentOrHidClick(detectsgg[0])
                }

                sleep.second(12)
                // agentOrHidback()


            }
        }

    }, '搜索赚金币', '搜索并浏览', '搜索浏览', '金币购划算')
    // ocrText('搜索赚金币', true)

    // if (!backUntilFindView(() => ocrText('日常任务', '详情▼', '现金:', '点就领', '立刻领'))) {
    //     enter_任务中心_KS(true)
    // }

    // backUntilFindView(() => ocrText('日常任务', '详情▼', '现金:', '点就领', '立刻领'))
}


// 进行浏览任务
function start_浏览任务_KS() {
    start_app_KS()

    skip_主界面的弹窗_KS()

    addExcuteFuncTimes('start_浏览任务_KS' + KS_CURRENT_FLAG)
    // agentOrHidClick(ocrTextInRegion(0, 0.85, 0.5, 1, '精选'))
    agentOrHidClickAbsoultX(ocrTextInRegion(0, 0.85, 0.5, 1, '精选'), screen.percentToWidth(0.3))

    //找色返回目标数组
    //比色是否成功
    var find = opencv.checkColorsEx('图色294182.cv');
    if (find == false) {
        // agentOrHidClick(ocrWaitForMoreTextMillisecondInRegion(0, 0.85, 0.5, 1, 4, '精选'))
        agentOrHidClickAbsoultX(ocrWaitForMoreTextMillisecondInRegion(0, 0.85, 0.5, 1, 4, '精选'), screen.percentToWidth(0.3))
    }

    if (getExcuteFuncTimes('start_浏览任务_KS' + KS_CURRENT_FLAG) < 3) {
        agentOrHidClick(ocrWaitForText('点击翻倍'))
    }

    //直播间
    // var detects = opencv.findImagesEx('图色844523.cv');
    // if (detects) {
    //     agentSwipDown()
    //     sleep.millisecond(1500)
    //     detects = opencv.findImagesEx('图色844523.cv');
    // }
    var detects = opencv.findImagesEx('图色362056.cv');
    if (detects == null) {
        skip_主界面的弹窗_KS()
    }

    // detects = opencv.findImagesEx('图色362056.cv');
    // if (detects == null) {
    //     press()
    //     agentOrHidClickOffectY(ocrWaitForText('不感兴趣'), 0)

    // }
    // agentSwipBig()
    // sleep.second(2)


    // detects = opencv.findImagesEx('图色362056.cv');
    // if (detects == null) {
    //     press()
    //     agentOrHidClickOffectY(ocrWaitForText('不感兴趣'), 0, 2)
    //     agentClickRight(ocrText('不喜欢'))
    //     detects = opencv.findImagesEx('图色362056.cv');
    // } else {
    //     click(0.5, 0.4)
    // }

    skip_主界面的弹窗_KS()

    //找图返回目标数组
    var detectsenter = opencv.findImagesEx('图色523634.cv');
    if (detectsenter != null) {
        agentOrHidClickOffectY(detectsenter[0], 20)
    }

    //找图返回目标数组
    var detectsclose = opencv.findImagesEx('图色507871.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }

    if (!ocrText('直播', '了解', '咨询', '广告', '下载', '图文', '长图')) {
        // 节省流量
        press(0.5, 0.5)
        if (!ocrWaitForMoreTextMillisecond('分享给', '推荐给', '不感兴趣', 3)) {
            skip_主界面的弹窗_KS()
        }
        if (!ocrWaitForMoreTextMillisecond('分享给', '推荐给', '不感兴趣', 3)) {
            backUntilFindView(() => ocrText('日常任务', '详情▼', '现金:', '点就领', '立刻领'))
        }
        moveUpSmaller2FindWid((findView) => {
            agentOrHidClickAbsoultX(findView, screen.getScreenWidth() - 200)
            click(0.5, 0.3)
        }, '省流模式', '模式', '省流')

        click(0.5, 0.3)
    }


}


function start_收集金币_KS() {
    start_app_KS()

    currentAppPackName = PACK_NAME_KS

    enter_任务中心_KS()

    startThredTaskMaxTime超时结束(() => {
        open_签到_KS()

        open_开宝箱_KS()

        if (!isIncenterPage_KS()) {
            enter_任务中心_KS(true)
        }

        watch_看广告_KS()

        if (!isIncenterPage_KS()) {
            enter_任务中心_KS(true)
        }

        go_点赞_KS()

        go_评论_KS()

        go_吃饭_KS()

        go_搜索_KS()
    })



    addExcuteFuncTimes('start_收集金币_KS')

    stopAllApp()


    // closeApp('com.smile.gifmaker')
}

